package com.aier.user.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.common.common.CommonSession;
import com.aier.common.common.Constant;
import com.aier.common.common.ICommonDAO;
import com.aier.user.entity.Follow;
import com.aier.user.entity.Fresh;
import com.aier.user.entity.Friend;
import com.aier.user.service.ICloseFriendService;
import com.aier.user.service.IHomeForumsService;
import com.aier.user.service.IUserService;

@Scope("prototype")
@Component("userChumsCircleAction")
public class UserChumsCircleAction extends UserBaseAction {

	/**
	 * 我的密友,与我关注的区分
	 */
	private static final long serialVersionUID = 1L;

	@Resource(name = "homeForumsService")
	private IHomeForumsService homeForumsService;

	@Resource(name = "closeFriendService")
	private ICloseFriendService closeFriendService;
	@Resource(name = "userService")
	private IUserService userService;

	// 错误码
	private int s;
	// 页数
	private int page;
	// 每页的记录数
	private int page_size;
	// 总页数
	private int page_count;
	// 总记录数
	private int count;

	// 新鲜事列表
	private List<Fresh> freshList;

	// 被加密友的用户id
	private Long auid;
	// 添加密友时的内容
	private String content;
	// 备注名
	private String remark;
	// 取消密友的用户id
	private String auidStr;
	// 取消密友时是否同时取消关注(0否1是)
	private Short flag;

	// 所有的密友列表简介
	private List<Friend> friendList;

	private Long uid;
	// 关注单非密友
	private List<Follow> followList;
	//关系
	private Integer type;//1.关注，2好友3.粉丝
	
	@Override
	public String execute() {

		return SUCCESS;
	}

	/**
	 * 查询密友的新鲜事
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String searchAllFresh() {
		if (page <= 0) {
			page = 1;
		}
		if (page_size <= 0) {
			page_size = Constant.num_10;
		}
		Map<String, Object> map = homeForumsService.searchFriendFresh(
				page,
				page_size,
				(Long) request.getSession().getAttribute(
						CommonSession.SESSION_ACCOUNT_ID));

		if (null != map) {
			page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
			count = (Integer) map.get(ICommonDAO.COUNT);
			page = (Integer) map.get(ICommonDAO.PAGE);

			freshList = (List<Fresh>) map.get(ICommonDAO.OBJ_LIST);

			if (null != freshList && freshList.size() > 0) {
				for (Fresh fresh : freshList) {
					if (fresh.getType() != 0) {
						fresh.setFresh(homeForumsService.searchFresh(
								fresh.getFid(), 1));
					}
				}
			}

		}
		uid = (Long) request.getSession().getAttribute(
				CommonSession.SESSION_ACCOUNT_ID);
		s = 1;
		return SUCCESS;
	}

	/**
	 * 添加密友
	 * 
	 * @return
	 */
	public String addFriend() {
		s = closeFriendService.insertFriend((Long) request.getSession()
				.getAttribute(CommonSession.SESSION_ACCOUNT_ID), auid, content);
		return SUCCESS;
	}

	/**
	 * 修改关注或密友的备注名
	 * 
	 * @return
	 */
	public String updateRemark() {
		s = closeFriendService.updateRemark((Long) request.getSession()
				.getAttribute(CommonSession.SESSION_ACCOUNT_ID), auid, remark,type);
		return SUCCESS;
	}

	/**
	 * 修改关注或密友的备注名App端
	 * 
	 * @return
	 */
	public String updateRemarkApp() {
		
		s = closeFriendService.updateRemark(uid, auid, remark, type);
		return SUCCESS;
	}

	/**
	 * 取消密友（批量）
	 * 
	 * @return
	 */
	public String cancelFriend() {
		s = closeFriendService.deleteFriend((Long) request.getSession()
				.getAttribute(CommonSession.SESSION_ACCOUNT_ID), auidStr, flag);
		return SUCCESS;
	}

	/**
	 * 获取所有密友
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String searchAllFriend() {
		if (page <= 0) {
			page = 1;
		}
		if (page_size <= 0) {
			page_size = Constant.num_10;
		}

		Map<String, Object> map = closeFriendService.searchAllFriend(
				page,
				page_size,
				(Long) request.getSession().getAttribute(
						CommonSession.SESSION_ACCOUNT_ID));
		if (null != map) {
			page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
			count = (Integer) map.get(ICommonDAO.COUNT);
			page = (Integer) map.get(ICommonDAO.PAGE);
			friendList = (List<Friend>) map.get(ICommonDAO.OBJ_LIST);
		}
		s = 1;
		return SUCCESS;
	}

	/**
	 * 获取关注但不是密友的列表
	 * 
	 * @return
	 */
	public String searchFollowNotFriend() {
		followList = closeFriendService.searchFollowNotFriend((Long) request
				.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID));
		s = 1;
		return SUCCESS;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPage_size() {
		return page_size;
	}

	public void setPage_size(int page_size) {
		this.page_size = page_size;
	}

	public int getS() {
		return s;
	}

	public List<Fresh> getFreshList() {
		return freshList;
	}

	public void setHomeForumsService(IHomeForumsService homeForumsService) {
		this.homeForumsService = homeForumsService;
	}

	public void setCloseFriendService(ICloseFriendService closeFriendService) {
		this.closeFriendService = closeFriendService;
	}

	public void setAuid(Long auid) {
		this.auid = auid;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public void setAuidStr(String auidStr) {
		this.auidStr = auidStr;
	}

	public void setFlag(Short flag) {
		this.flag = flag;
	}

	public List<Friend> getFriendList() {
		return friendList;
	}

	public Long getUid() {
		return uid;
	}

	public void setUid(Long uid) {
		this.uid = uid;
	}

	public int getPage_count() {
		return page_count;
	}

	public int getCount() {
		return count;
	}

	public List<Follow> getFollowList() {
		return followList;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public void setType(Integer type) {
		this.type = type;
	}
	

}
